/**
 * 
 */
package labox.innovation.gameserver.network.clientpackets;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.gameserver.model.ConsignmentItem;
import labox.innovation.gameserver.model.ConsignmentManager;
import labox.innovation.gameserver.model.L2ItemInstance;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.model.itemcontainer.ConsignmentWarehouse;
import labox.innovation.gameserver.model.itemcontainer.PcInventory;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.serverpackets.ConsignmentUIPacket;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;

/**
 * @author ljsh
 * @date 2010-5-4
 * @version 1.0
 * @description 物品上架
 */
public class RequestItemShelve extends L2GameClientPacket {
	private static Logger _log = LoggerFactory.getLogger(RequestItemShelve.class);
	private static final String _C_REQUESTITEMSHELVE = "_C_RequestItemShelve";

	private static String optionName = "Articles Added";

	/**
	 * 物品ID
	 */
	private long _itemid;

	/**
	 * 寄售时长
	 */
	private int _consignmentLength;

	/**
	 * 价格
	 */
	private int _price;

	/**
	 * 价格类型
	 */
	private int _priceType;

	/**
	 * 叠加数量
	 */
	private int _count;

	/**
	 * 手续费
	 */
	private int _handlingFee;

	/*
	 * (non-Javadoc)
	 * @see labox.innovation.gameserver.network.clientpackets.L2GameClientPacket#getType()
	 */
	@Override
	public String getType() {
		return _C_REQUESTITEMSHELVE;
	}

	/*
	 * (non-Javadoc)
	 * @see labox.innovation.gameserver.network.clientpackets.L2GameClientPacket#readImpl()
	 */
	@Override
	protected void readImpl() {
		_itemid = readLong();
		int t = readByte();
		_price = readInt();
		_priceType = readInt();
		_count = readInt();
		_consignmentLength = ConsignmentManager.getConsgnmentLength(t);
		_handlingFee = ConsignmentManager.calculateHandleFee(t, _price);
	}

	/*
	 * (non-Javadoc)
	 * @see labox.innovation.gameserver.network.clientpackets.L2GameClientPacket#runImpl()
	 */
	@Override
	protected void runImpl() {

		FuzePcInstance active = getClient().getActiveChar();
		boolean enough = false;
		if (active == null) {
			return;
		}
		ConsignmentWarehouse house = ConsignmentManager.getConsignmentWarehouse(active.getObjectId());
		if (house == null) {
			house = new ConsignmentWarehouse(active.getObjectId());
			ConsignmentManager.addConsignmentWarehouse(house);
		}
		if (house.isFull()) {
			active.sendPacket(new SystemMessage(SystemMessageId.CONS_NOENOUGHSPACE));
			return;
		}
		if (_price > ConsignmentManager.PRICELIMIT) {
			active.sendPacket(new SystemMessage(SystemMessageId.CONS_OUTPRICELIMIT));
			return;
		}
		if (_handlingFee == -1) {// 当计算出来的手续费是-1时，则是非法操作，直接返回
			return;
		}
		int money = 0;
		// if (_priceType == PcInventory.MONEY_SLIVER_ID) {
		// money = active.getInventory().getMoneySilver();
		// }
		if (_priceType == PcInventory.MONEY_NOTE_ID) {
			money = active.getInventory().getMoneyNote();
		}
		// else if (_priceType == PcInventory.MONEY_YAUNBAO_ID) {
		// money = active.getInventory().getMoneyYuanBao();
		// }
		else {
			return;// 非法价格类型返回
		}
		if (money >= _handlingFee) {
			enough = true;
		} else {
			active.sendPacket(new SystemMessage(SystemMessageId.CONS_NOMONEY));
			return;
		}
		// 执行上架操作
		if (enough) {
			// if (_priceType == PcInventory.MONEY_SLIVER_ID) {
			// active.getInventory().reduceMoneySilver(optionName, _handlingFee, active, null);
			// }

			L2ItemInstance item = active.getInventory().getItemByObjectId(_itemid);
			// 检测是否是可以寄售物品
			if (!ConsignmentManager.isCanShevleItems(active, item)) {
				return;
			}
			// 物品是否可叠加的
			ConsignmentItem conitem = null;
			if (_count <= 0)
				return;
			if (item.getItem().isStackable()) {
				if (item.getCount() < _count) {
					// 物品没有足够数量的物品
					active.sendPacket(new SystemMessage(SystemMessageId.CONS_NOCOUNT));
					return;
				}
				if (item.getCount() == _count) {
					conitem = new ConsignmentItem(item, _consignmentLength, _price, _priceType, true);
					active.getInventory().removeItem(item);
					_log.info("物品上架:" + item.getName() + "(" + item.getObjectId() + ")," + _count + ";背包到寄售仓库", active.getObjectId(), active.getName());
					_log.info("物品上架:" + item.getName() + "(" + item.getObjectId() + ")," + _count + ";背包到寄售仓库", item.getObjectId(), item.getName());
				}
				else if(item.getCount() > _count) {
					conitem = new ConsignmentItem(house.createConsignmentItem(optionName, item.getItemId(), _count, active), _consignmentLength, _price, _priceType, true);
					item.changeCount(optionName, -_count, active, null);
					_log.info("物品上架:创建物品" + item.getName() + "(" + conitem.getItem().getObjectId() + ")," + _count + ";增加到寄售仓库", active.getObjectId(), active.getName());
					_log.info("物品上架:减少物品数量" + item.getName() + "(" + item.getObjectId() + ")," + _count + ";减少背包物品数量", item.getObjectId(), item.getName());
				}

			} else {
				active.getInventory().removeItem(item);
				conitem = new ConsignmentItem(item, _consignmentLength, _price, _priceType, true);
				_log.info("物品上架:" + item.getName() + "(" + item.getObjectId() + ")," + _count + ";背包到寄售仓库", active.getObjectId(), active.getName());
				_log.info("物品上架:" + item.getName() + "(" + item.getObjectId() + ")," + _count + ";背包到寄售仓库", item.getObjectId(), item.getName());
			}
			if (_priceType == PcInventory.MONEY_NOTE_ID) {
				active.reduceMoneyNote(optionName, _handlingFee, null, true);
				_log.info("物品上架费用:银票(" + _priceType + ")," + _handlingFee, active.getObjectId(), active.getName());
				_log.info("物品上架费用:银票(" + _priceType + ")," + _handlingFee, _priceType, "银票");
			}
			if (_priceType == PcInventory.MONEY_YAUNBAO_ID) {
				active.reduceMoneyYuanBao(optionName, _handlingFee, null, true);
				_log.info("物品上架费用:元宝(" + _priceType + ")," + _handlingFee, active.getObjectId(), active.getName());
				_log.info("物品上架费用:元宝(" + _priceType + ")," + _handlingFee, _priceType, "元宝");
			}
			ConsignmentManager.initConsignmentItem(house, conitem);
			active.sendPacket(new ConsignmentUIPacket(active));
			active.sendPacket(new SystemMessage(SystemMessageId.SHELVE_SUCCESS));// 发送上架成功消息
		}

	}
}
